package com.pro.util.generator;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

public class GenerateUtil {
	/*
	 * public static String BASE_PACKAGE = "/com/pro/test/"; public static String
	 * MAPPER_TEMPLATE_NAME = "mybatis_generator_mapper.xml"; public static
	 * String BASE_BEAN = "/com/pro/test/bean/"; public static String
	 * BEAN_PACKAGE = "com.pro.test.bean"; public static String DAO_PACKAGE =
	 * "com.pro.test.dao"; public static String DAO_DESC_PAHT =
	 * "/com/pro/test/dao/"; public static String DAO_FILE_TEMPLATE_NAME =
	 * "${UtableName}Dao.java"; public static String DAO_TEMPLATE_NAME =
	 * "DaoTemplate.tmp"; public static String BASE_DAO_TEMPLATE_NAME =
	 * "BaseDao.tmp"; public static String BASE_DAO_NAME = "BaseDao.java";
	 * public static String INTERFACE = "interface"; public static String
	 * TEMPLATE_PATH = "/com/pro/util/generator/"; public static String
	 * MAPPER_DESC_PATH = "/mybatis_mapper/"; public static String
	 * MAPPER_FILE_TEMPALATE_NAME = "mybatis_${tableName}_mapper.xml"; public
	 * static String tableName = "t_polling";//鐢熸垚琛ㄧ粨鏋�
	 */
	public static String BASE_PATH = "/com/pro/";
	public static String BASE_PACKAGE="";
	public static String BASE_BEAN = "";
	public static String BEAN_PACKAGE = "";
	public static String DAO_PACKAGE = "";
	public static String DAO_DESC_PAHT = "";
	public static String TEMPLATE_PATH = "/com/pro/util/generator/";
	public static String MAPPER_TEMPLATE_NAME = "mybatis_generator_mapper.xml";
	public static String DAO_FILE_TEMPLATE_NAME = "${UtableName}Dao.java";
	public static String DAO_TEMPLATE_NAME = "DaoTemplate.tmp";
	public static String BASE_DAO_TEMPLATE_NAME = "BaseDao.tmp";
	public static String BASE_DAO_NAME = "BaseDao.java";
	public static String INTERFACE = "interface";
	public static String SOURCE_FILE = File.separator + "sources" + File.separator;
	public static String MAPPER_DESC_PATH = File.separator + "mybatis_mapper" + File.separator;
	public static String MAPPER_FILE_TEMPALATE_NAME = "mybatis_${tableName}_mapper.xml";
	public static String tableName = "t_polling";

	public static void initName() {
		String foldPath = "";
		Properties properties = MyBatisGenerator.getProperties();
		BASE_PACKAGE = properties.getProperty("base_package");
		if (BASE_PACKAGE.contains(".")) {
			foldPath = File.separator + BASE_PACKAGE.replaceAll("\\.", "\\" + File.separator);
			BASE_PATH = foldPath;
		}
		BASE_BEAN = foldPath + File.separator + "bean" + File.separator;
		BEAN_PACKAGE = BASE_PACKAGE + ".bean";
		DAO_DESC_PAHT = foldPath + File.separator + "dao" + File.separator;
		DAO_PACKAGE = BASE_PACKAGE + ".dao";
	}

	public static void start(List<Table> tableList) throws Exception {
		initName();
		for (Table table : tableList) {
			if (tableName != null && table.getTableName().equals(tableName)) {
				generateTable(table);
			}
			if (tableName != null) {
				generateTable(table);
			}
		}
	}

	public static void generateTable(Table table) throws Exception {
		table.setDaoPackage(DAO_PACKAGE);
		table.setBeanPackage(BEAN_PACKAGE);
		generateMapper(table);
		generateClassFile(table);
		generateBaseDao(table);
		generateDao(table);
		generateMybatisFile();
	}

	public static void generateMapper(Table table) throws Exception {
		// table.setPackageName(base);
		String src = getSourcePath(MAPPER_TEMPLATE_NAME);
		MapperReader mapperReader = new MapperReader(src, table);
		mapperReader.setBasePackage(BASE_PACKAGE);
		String value = mapperReader.readFileString();
		String desc = getResourcePath() + MAPPER_DESC_PATH + MAPPER_FILE_TEMPALATE_NAME.replaceAll("\\$\\{tableName\\}",
				table.getTableName().toLowerCase().replace("t_", ""));
		FileUtil.writeToFile(desc, value);
	}

	public static void generateClassFile(Table table) throws Exception {
		ClassBean classBean = new ClassBean();
		classBean.setColumnList(table.getColumnList());
		classBean.setClassName(table.getuTableName());
		classBean.setPackageName(BEAN_PACKAGE);
		String desc = getCurrentPath() + BASE_BEAN;
		FileUtil.writeToFile(desc + table.getuTableName() + ".java", classBean.toString());
	}

	public static void generateDao(Table table) throws Exception {
		String src = getSourcePath(DAO_TEMPLATE_NAME);
		MapperReader mapperReader = new MapperReader(src, table);
		mapperReader.setBeanPackage(BEAN_PACKAGE);
		mapperReader.setDaoPackage(DAO_PACKAGE);
		mapperReader.setClassType(INTERFACE);
		String value = mapperReader.readFileString();
		String desc = getCurrentPath() + DAO_DESC_PAHT
				+ DAO_FILE_TEMPLATE_NAME.replaceAll("\\$\\{UtableName\\}", table.getuTableName());
		FileUtil.writeToFile(desc, value);
	}

	public static String[] fileList = { "GroupBy.tmp","MyBatisExpression.tmp","Order.tmp", "ListPage.tmp", "Page.tmp", "PageResult.tmp",
			"SearchCondition.tmp" };

	/**
	 * 生成mybatis的依赖文件
	 * 
	 * @throws IOException
	 */
	public static void generateMybatisFile() throws Exception {
		for (String str : fileList) {
			String path = getSourcePath(str);
			MapperReader mapperReader = new MapperReader(path, new Table());
			mapperReader.setBasePackage(BASE_PACKAGE);
			String value = mapperReader.readFileString();
			String desc = getCurrentPath() + File.separator + BASE_PATH+File.separator+"mybatis" + File.separator + str;
			desc = desc.replaceAll(".tmp", ".java");
			FileUtil.writeToFile(desc, value);
		}
	}

	public static void generateBaseDao(Table table) throws Exception {
		String src = getSourcePath(BASE_DAO_TEMPLATE_NAME);
		MapperReader mapperReader = new MapperReader(src, table);
		mapperReader.setBeanPackage(BEAN_PACKAGE);
		mapperReader.setBasePackage(BASE_PACKAGE);
		String value = mapperReader.readFileString();
		String desc = getCurrentPath() + DAO_DESC_PAHT
				+ BASE_DAO_NAME.replaceAll("\\$\\{UtableName\\}", table.getuTableName());
		FileUtil.writeToFile(desc, value);
	}

	public static String getCurrentPath() {
		String path = new File("src").getAbsolutePath();
		String desc = path.replaceAll("\\\\", "\\" + File.separator);
		return desc;
	}

	public static String getResourcePath() {
		String path = new File("resources").getAbsolutePath();
		System.out.println("path:" + path);
		String desc = path.replaceAll("\\\\", "\\" + File.separator);
		return desc;
	}
	public static String getSourcePath(String sourceName) {
		String filePath =sourceName;
		return filePath;
	}
	public static String getSourcePathBak(String sourceName) {
		String path = new File("sources").getAbsolutePath();
		String source = path.replaceAll("\\\\", "\\" + File.separator);
		String filePath = source + File.separator + sourceName;
		return filePath;
	}
}
